import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/request/login_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_center_alert_widget.dart';
import 'package:flutter_app_unionad/ui/basice/input_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/ui/my_facility/cut_device_page.dart';
import 'package:flutter_app_unionad/view_model/change_device_event.dart';

//绑定设备
class PromotionInputWidget extends StatefulWidget {
  final TextInputType ?keyboardKey;
  final String title;
  final Function(String value, String devId)?onTap;
  final String buttonTitle;
  final bool isExchange;

  PromotionInputWidget({
    this.keyboardKey,
    this.title = '请确认兑换天数',
    this.onTap,
    this.buttonTitle = '确定',
    this.isExchange = false,
  });

  @override
  _PromotionInputWidgetState createState() => _PromotionInputWidgetState();
}

class _PromotionInputWidgetState extends State<PromotionInputWidget> with BaseCenterAlertWidget {
  String? device;
  String? text;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print("ddddd${widget.isExchange}");
    if (widget.isExchange) {
      text = UserCacheUil.getString(Configuration.deviceId);
    }
  }

  @override
  Widget buildContent(BuildContext context) {
    return Column(
      children: [
        MySizeBox(height: 30),
        Text(
          widget.title,
          style: TextStyle(
            color: MyColors.color_27_27_27,
            fontSize: MyFont.font16,
            fontWeight: MyFontWeight.bold,
          ),
        ),
        MySizeBox(height: 10),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 40.px),
          child: InputSeparatorWidget(
            valueChange: (value) {
              device = value;
            },
            title: "",
            keyboard: widget.keyboardKey ?? TextInputType.numberWithOptions(decimal: true),
            placeholder: "请输入",
            titleWidth: 0,
            inputFont: MyFont.font16,
          ),
        ),
        if (widget.isExchange) MySizeBox(height: 10),
        if (widget.isExchange)
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 40.px),
            child: Row(
              children: [
                Text(
                  '兑换设备:',
                  style: TextStyle(color: MyColors.color_27_27_27, fontWeight: FontWeight.w400, fontSize: 14.px),
                ),
                Text(
                  text??"",
                  style: TextStyle(color: MyColors.color_247_73_60, fontWeight: FontWeight.w400, fontSize: 14.px),
                ),
                Expanded(child: SizedBox()),
                GestureDetector(
                  onTap: () {
                    NavigatorUtil.push(context, CutDevicePage.routeName).then((value) {
                      if (value != null && mounted) {
                        setState(() {
                          text = value;
                        });
                      }
                    });
                  },
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "切换设备",
                        style: TextStyle(
                          color: MyColors.color_27_27_27,
                          fontSize: MyFont.font14,
                          fontWeight: MyFontWeight.regular,
                        ),
                      ),
                      Icon(
                        Icons.swap_horiz,
                        size: 16.px,
                        color: MyColors.color_27_27_27,
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        MySizeBox(height: 30),
        MyButton(
          title: widget.buttonTitle,
          radius: 1.px,
          onTap: () {
            if (widget.onTap != null) widget.onTap?.call(device??'', text??'');
          },
          width: 133.px,
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return buildCloseAlertWidget(context, child: buildContent(context));
  }
}
